package Common
{
	import Base.Loader.TextureLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	
	public class TextureResManager
	{
		public var dataObject:Object = null;
		
		public var loader:Object = null;
		public var loadCount:int = 0;
		
		public var lineArray:Array = null;
		
		public var stop:Boolean = false;
		public var isLoading:Boolean = false;
		
		
		public function TextureResManager()
		{
			
		}
		
		
		public function init():void
		{
			lineArray = new Array();
			dataObject = new Object();
			
			loader = new Object();
		}
		
		
		public function addBitmapData( path:String , bd:BitmapData ):void
		{
			dataObject[ path ] = bd;
		}
		
		
		public static var instance:TextureResManager = null;
		public static function Instance():TextureResManager
		{
			if ( instance == null )
			{
				instance = new TextureResManager();
			}
			
			return instance;
		}
		
		
		public function clear():void
		{
			while( lineArray.length )
			{
				var path:String = lineArray[ 0 ].loader.path;
				
				loader[ path ].loader.contentLoaderInfo.removeEventListener( Event.COMPLETE , onComplete );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
				loader[ path ].loader.Release();
				
				delete loader[ path ].loader;
				delete loader[ path ].fun;
				delete loader[ path ];
				
				lineArray.shift();
			}
			
			loadCount = 0;
			UILoading.instance.visible( false );
			
			isLoading = false;
		}
		
		
		public function stopLoading():void
		{
			stop = true;
		}
		
		
		public function startLoading():void
		{
			stop = false;
			
			if ( lineArray.length && !isLoading )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				
			}
		}
		
		
		public function load( path:String , fun:Function , loading:Boolean = false ):void
		{
			//loading = true;
			
			if ( loader[ path ] )
			{
				loader[ path ].loading = loading;
				loader[ path ].fun.push( fun );
				return;
			}
			
			if ( dataObject[ path ] )
			{
				fun();
				return;
			}
			
			loadCount++;
			
			var l:TextureLoader = new TextureLoader();
			l.contentLoaderInfo.addEventListener( Event.COMPLETE , onComplete );
			l.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR , onError );
			l.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			l.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS , onProgress );
			
			l.path = path;
			
			loader[ path ] = new Object();
			loader[ path ].loader = l;
			loader[ path ].loading = loading;
			loader[ path ].fun = new Array();
			loader[ path ].fun.push( fun );
			
			lineArray.push( loader[ path ] );
			
			if ( lineArray.length == 1 && !stop && !isLoading )
			{
				l.Load( path );
				isLoading = true;
			}
			
			if ( loading )
			{
				UILoading.instance.visible( true );
				UILoading.instance.setText( 0 , loadCount , "" , "" );
			}
		}
		
		
		public function onProgress( event:ProgressEvent ):void
		{
			
		}
		
		
		public function onSecurityError( event:SecurityErrorEvent ):void
		{
			isLoading = false;
			loadCount--;
			
			lineArray.shift();
			
			var b:Boolean = false;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					b = true;
				}
			}
			UILoading.instance.visible( b );
			
			if ( lineArray.length && !stop )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				
			}
			
			for ( i = 0 ; i < loader[ event.target.loader.path ].fun.length ; i++ )
			{
				if ( loader[ event.target.loader.path ].fun[ i ] != null )
				{
					loader[ event.target.loader.path ].fun[ i ]( null );
				}
			}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.COMPLETE , onComplete );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
		}
		
		
		public function onError( event:IOErrorEvent ):void
		{
			isLoading = false;
			loadCount--;
			
			lineArray.shift();
			
			var b:Boolean = false;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					b = true;
				}
			}
			UILoading.instance.visible( b );
			
			if ( lineArray.length && !stop )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				
			}
			
			for ( i = 0 ; i < loader[ event.target.loader.path ].fun.length ; i++ )
			{
				if ( loader[ event.target.loader.path ].fun[ i ] != null )
				{
					loader[ event.target.loader.path ].fun[ i ]( null );
				}
			}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.COMPLETE , onComplete );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
		}
		
		
		public function onComplete( event:Event ):void
		{
			isLoading = false;
			loadCount--;
			
			lineArray.shift();
			
			var b:Boolean = false;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					b = true;
				}
			}
			UILoading.instance.visible( b );
			
			if ( lineArray.length && !stop )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				
			}
			
			var image:Bitmap = Bitmap( event.target.content );
			dataObject[ event.target.loader.path ] = image.bitmapData;
			
			for ( i = 0 ; i < loader[ event.target.loader.path ].fun.length ; i++ )
			{
				if ( loader[ event.target.loader.path ].fun[ i ] != null )
				{
					loader[ event.target.loader.path ].fun[ i ]( image.bitmapData );
				}
			}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onComplete );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
		}
		
		
		public function getBitmapData( path:String ):BitmapData
		{
			if ( dataObject[ path ] )
			{
				return dataObject[ path ];
			}
			
			return null;
		}
		
		
	}
}